import { computed, onMounted, Ref, ref } from 'vue'
import { getTermListAll } from '../api'
import { Term } from '/@/types/Term'

export function useSearchPopup() {
  const semesterList = useSemester()

  const yearlyList = useYearly(semesterList)

  return { yearlyList, semesterList }
}

export function useYearly(semesterList: Ref<Term[]>) {
  return computed(() => [
    ...new Set(semesterList.value.map(({ yearly }) => yearly)),
  ])
}

export function useSemester() {
  const semesterList = ref<Term[]>([])

  onMounted(async () => {
    const {
      data: { data },
    } = await getTermListAll()
    semesterList.value = data
  })

  return semesterList
}
